<?php
namespace AdminApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class OrderApiController extends AbstractRestfulController
{
    public function getList()
    {
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('Models\Model\UserMedicineOrderFactory');
		$allUserOrders = $userMedicineOrderTable->getAllAdminOrders('all')->toArray();
		if(count($allUserOrders)!=0){
				return new JsonModel(array(
					'value'			=>'1',
					'orders'		=> json_encode($allUserOrders),
				));
			}else{
				return new JsonModel(array(
					'value'			=>'0',
					'orders'		=> 	'Orders list is empty',
				));
			}
    }
    public function get($id)
    {	
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('User\Model\UserMedicineOrderFactory');
		$explodeId=explode('-',$id);
		if($explodeId[0]=='user'){
			$user_id=$explodeId[1];
			$ordersList=array();
			$userMedicineOrderTable=$this->getServiceLocator()->get('User\Model\UserMedicineOrderFactory');	
			$totalOrders = $userMedicineOrderTable->totalOrders($user_id);
			if(count($totalOrders)!=0){
				return new JsonModel(array(
					'value'			=>	1,
					'orders'		=> json_encode($totalOrders->toarray()),
				));
			}else{
				return new JsonModel(array(
					'value'			=>	0,
					'orders'		=> 	'Orders list is empty',
				));
			}
		}else{
			$order_id=$explodeId[1];
			$checkOrder = $userMedicineOrderTable->checkOrder($order_id);
			if($checkOrder!=0){
				$orderStatus = $userMedicineOrderTable->orderStatus($order_id);
				if($orderStatus->current()->status==0){
					return new JsonModel(array(
						'status'		=> 	'pending',
					));
				}else if($orderStatus->current()->status==1){
					return new JsonModel(array(
						'status'		=> 'processing',
					));
				}else if($orderStatus->current()->status==2){
					return new JsonModel(array(
						'status'		=> 'processed',
					));
				}else{
					return new JsonModel(array(
						'status'		=> 'cancelled',
					));
				}
			}else{
				return new JsonModel(array(
					'status'		=> 'Invalid Tracking Id',
				));
			}
		}
    }
    public function create($oder)
    {
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('User\Model\UserMedicineOrderFactory');
		if($oder['oldorderId']==0){
			$sendOrder = $userMedicineOrderTable->saveOrder($oder);
		}else{
			$getOrderDetails = $userMedicineOrderTable->getOrderDetails($order['oldorderId']);
			$orderNew=$getOrderDetails->toarray();
			$sendOrder = $userMedicineOrderTable->saveReOrder($orderNew[0]);
			$sendOrderr = $userMedicineOrderTable->changeAutoRenewalReorder($order['oldorderId']);
		}
		if($sendOrder!=0){
			return new JsonModel(array(
				'Tracking Id'	=>	$sendOrder,
				'success'		=> 	'success',
			));
		}else{
			return new JsonModel(array(
				'status'		=> 'not success',
			));
		}
    }
    public function update($order_id,$order)
    {
		header('Access-Control-Allow-Origin: *');
		$userMedicineOrderTable=$this->getServiceLocator()->get('User\Model\UserMedicineOrderFactory');	
		$checkOrder = $userMedicineOrderTable->checkOrder($order_id);
		if($checkOrder!=0){
			if($order['type']==1){
				$updateOrderRenewal = $userMedicineOrderTable->updateOrderRenewal($order_id,$order['auto_renewal']);
				return new JsonModel(array(
					'status'			=>	'success',
				));
			}else{
				$orderStatus = $userMedicineOrderTable->orderStatus($order_id);
				if($orderStatus->current()->status==0 || $orderStatus->current()->status==1){
					$cancelledOrder = $userMedicineOrderTable->cancelledOrder($order_id);
					$orderDeliveryTable=$this->getServiceLocator()->get('Models\Model\OrderDeliveryDetailsFactory');
					$getOrderDetails = $orderDeliveryTable->cancelledOrder($order_id);
					return new JsonModel(array(
						'status'			=>	'success',
					));
				}else{
					return new JsonModel(array(
						'status'			=>	'You are not cancelled the order because order is processing',
					));
				}
			}
		}else{
			return new JsonModel(array(
				'status'			=>	'Invalid Order Id',
			));
		}
    }
    public function delete($id)
    {
       
    }
}